<?php
class User_Model_Right 
{
 
	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new User_Model_DbTable_Right();
	}
	
	/**
	*add One Row  
	*@param array $row 
	*@return boolean
	*/
	public function add($row){
		return $this->_table->insert($row);
	}
	/**
	*Update One Row  
	*@param rowSet $row Customer's data 
	*@param int $customer_id 
	*@return boolean
	*/
	public  function update($row,$ur_id){	
		$where = $this->_table->getAdapter()->quoteInto('ur_id = ?', $ur_id); 
		return $this->_table->update($row, $where);		
	}
		
	/**
	*Delete One Row
	*@param int $ur_id
	*@return boolean
	*/
	public function delete($ur_id){	
		$where = $this->_table->getAdapter()->quoteInto('ur_id = ?', $ur_id); 
		return $this->_table->delete($where);		
	}
			
	/**
	*Get Rowset By ur_id
	*@param int $ur_id
	*@return Rowset
	*/
	public function getByUrid($id){
		$select = $this->_table->select()->where('ur_id = ?', $id);	 
		return $this->_table->fetchRow($select)->toArray();
	}
	
	/**
	*Get Rowset
	*@param 
	*@return Rowset
	*/
	public function getAllModule(){
		$select = $this->_table->select()
				->from('user_right', 'ur_module')
				->group('ur_module');
		return $this->_table->fetchAll($select)->toArray();
	}
	
	/**
	 * 根据条件获取指定模块信息
	 */
	public function getByCond($cond,$type,$order='ur_id',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['ur_name']) && $cond['ur_name']){
			$select->where('ur_name = ?',$cond['ur_name']);
		}
		if(isset($cond['ur_module']) && $cond['ur_module']){
			$select->where('ur_module = ?',$cond['ur_module']);
		}
		$select->order($order);
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		
		$select->limit($count,$offset);
		$rowset = $this->_table->fetchAll($select);
		return $rowset->toArray();	
	}
	
}